package com.jml.适配器Adapt008.servlet;


import com.jml.适配器Adapt008.adapter.HandlerAdapter;
import com.jml.适配器Adapt008.adapter.impl.AnnotationHandlerAdapter;
import com.jml.适配器Adapt008.adapter.impl.HttpRequestHandlerAdapter;
import com.jml.适配器Adapt008.controller.Handler;
import com.jml.适配器Adapt008.controller.impl.AnnotationHandler;
import java.util.ArrayList;
import java.util.List;

/**
 * @author:jml
 * @date: 2019/11/11 15:41
 * @description：
 */
public class DispatcherServlet {
	
	private List<HandlerAdapter> handlerAdapters;
	
	public DispatcherServlet() {
		handlerAdapters = new ArrayList<HandlerAdapter>();
		handlerAdapters.add(new HttpRequestHandlerAdapter());
		handlerAdapters.add(new AnnotationHandlerAdapter());
	}
	
	public void dispatcher() {
		// 1. 假设已经已经获取到hanlder是AnnotationHandler
		Handler hanlder = new AnnotationHandler();
		
		/*
			// 1. 假设已经已经获取到hanlder是HttpHandler
			Controller hanlder = new HttpHandler();
		*/
		
		// 2.获取具体适配器
		HandlerAdapter handlerAdapter = getHandlerAdapter(hanlder);
		// 3.执行我们的请求方案
		handlerAdapter.handle(hanlder);
	}
	
	public HandlerAdapter getHandlerAdapter(Handler handler) {
		if (this.handlerAdapters != null) {
			for (HandlerAdapter ha : this.handlerAdapters) {
				if (ha.supports(handler)) {
					return ha;
				}
			}
		}
		return null;
	}
}
